package mya_dc.compilation_server.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import mya_dc.shared_classes.gui.SharedComponents;

public class ConnectionCreator extends JDialog 
{
	private static final long serialVersionUID = 1L;
	private JTextField mainPortField;
	private JTextField mainAddressField;
	private JTextField userPortField;
	private JFrame frame;

	public ConnectionCreator() 
	{
		super();
		setResizable(false);
		getContentPane().setLayout(null);
		setTitle("Connection Setup");
		setModalityType(ModalityType.DOCUMENT_MODAL);
		setModal(true);
		setBounds(100, 100, 256, 282);

		final JLabel userConnectionPortLabel = new JLabel();
		userConnectionPortLabel.setText("User Connections:");
		userConnectionPortLabel.setBounds(23, 22, 155, 16);
		getContentPane().add(userConnectionPortLabel);

		userPortField = new JTextField();
		userPortField.setBounds(91, 53, 114, 20);
		userPortField.setHorizontalAlignment(JTextField.CENTER);
		userPortField.setText("8000");
		getContentPane().add(userPortField);

		final JLabel portLabel = new JLabel();
		portLabel.setText("port:");
		portLabel.setBounds(35, 55, 66, 16);
		getContentPane().add(portLabel);

		final JLabel masterServerAddressLabel = new JLabel();
		masterServerAddressLabel.setText("Master Server Address:");
		masterServerAddressLabel.setBounds(23, 91, 135, 16);
		getContentPane().add(masterServerAddressLabel);

		final JLabel hostLabel = new JLabel();
		hostLabel.setText("host:");
		hostLabel.setBounds(35, 125, 66, 16);
		getContentPane().add(hostLabel);

		mainAddressField = new JTextField();
		mainAddressField.setBounds(91, 120, 114, 20);
		mainAddressField.setHorizontalAlignment(JTextField.CENTER);
		mainAddressField.setText("132.69.226.179");
		getContentPane().add(mainAddressField);

		final JLabel portLabel_1 = new JLabel();
		portLabel_1.setText("port:");
		portLabel_1.setBounds(35, 162, 66, 16);
		getContentPane().add(portLabel_1);

		mainPortField = new JTextField();
		mainPortField.setBounds(91, 160, 114, 20);
		mainPortField.setText("9200");
		mainPortField.setHorizontalAlignment(JTextField.CENTER);
		getContentPane().add(mainPortField);

		final JButton okButton = new JButton();
		okButton.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent arg0) {
				saveConnectionParameters();
			}
		});
		okButton.setText("OK");
		okButton.setBounds(133, 212, 93, 26);
		getContentPane().add(okButton);
		
		final JButton cancelButton = new JButton();
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent arg0) {
				doCancel();
			}
		});
		cancelButton.setText("Cancel");
		cancelButton.setBounds(23, 212, 93, 26);
		getContentPane().add(cancelButton);
		
		setVisible(true);
	}
	
	
	void saveConnectionParameters()
	{
		int nUserPort = 0;
		int nMainServerPort = 0;
		InetAddress MainServerAddress = null;
		boolean bError = false;
		try
		{
			String sUserPort = userPortField.getText();
			nUserPort =  Integer.parseInt(sUserPort);
			String sMainPort = mainPortField.getText();
			nMainServerPort = Integer.parseInt(sMainPort);
			if ((nUserPort < MIN_PORT) || (nUserPort > MAX_PORT))
				bError = true;
			if ((nMainServerPort < MIN_PORT) || (nMainServerPort > MAX_PORT))
				bError = true;
			MainServerAddress = InetAddress.getByName(mainAddressField.getText());
			
		}
		catch(NumberFormatException e)
		{
			bError = true;
		}
		catch(UnknownHostException e)
		{
			SharedComponents.MessageBox(frame, SharedComponents.EMessageBoxType.ERROR_ILLEGALEHOSTNAME, 0);
			return;
		}
		if (bError)
		{
			JOptionPane.showMessageDialog(frame,"One or more of the port numbers is invalid. Reinsert a port number in the range 1025 - 65535", "Error", 0);
			return;
		}
		else
		{
			try
			{
				CompilationServerGUI.setPortsAndAddress(nUserPort, nMainServerPort, MainServerAddress);
				m_Canceled = false;
				dispose();
			}
			catch (Exception e){}
		}		
	}
	
	void doCancel()
	{
		this.dispose();
	}
	public boolean isCanceled()
	{
		return m_Canceled;
	}
	private boolean m_Canceled = true;
	private static final int MIN_PORT = 1025;
	private static final int MAX_PORT = 65535;
}
